package com.aabte.weather.weatherredis.city;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.Reader;
import java.io.StringReader;

public class XmlBuilder {

    @SuppressWarnings("unchecked")
    public static <T> T xmlStrToObject(Class<T> clazz, String xmlStr) throws Exception {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        Reader reader = new StringReader(xmlStr);
        T xmlObject = (T) unmarshaller.unmarshal(reader);

        reader.close();

        return xmlObject;
    }

}
